package com.deer.wms.base.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.base.system.dao.AreaInfoMapper;
import com.deer.wms.base.system.model.Area.AreaInfo;
import com.deer.wms.base.system.model.Area.AreaInfoCriteria;
import com.deer.wms.base.system.model.Area.AreaInfoDto;
import com.deer.wms.base.system.service.AreaInfoService;
import com.deer.wms.project.root.service.SuperServiceImpl;
import com.deer.wms.system.manage.model.DataDictDetail;
import com.deer.wms.system.manage.model.ValidatorAreaInfoModel;
import com.deer.wms.system.manage.service.DataDictDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.deer.wms.project.root.util.CollectionUtils.convertMap;

/**
 * Created by guo on 2019/11/21.
 */
@Service
@Transactional
public class AreaInfoServiceImpl extends SuperServiceImpl<AreaInfoMapper, AreaInfo> implements AreaInfoService {

    @Autowired
    private AreaInfoMapper areaInfoMapper;
    @Autowired
    private DataDictDetailService dataDictDetailService;

    @Override
    public List<AreaInfo> findList(AreaInfoCriteria criteria) {
        List<AreaInfo> list = list();
        List<DataDictDetail> dataDictDetails = dataDictDetailService.list(new QueryWrapper<DataDictDetail>().eq("data_dict_code", "wareType"));
        Map<String, DataDictDetail> dataDictDetailMap = convertMap(dataDictDetails, DataDictDetail::getDictKey);

        for (AreaInfo areaInfo : list) {
            DataDictDetail dataDictDetail = dataDictDetailMap.get( Convert.toStr(areaInfo.getWaveType()));
            if (ObjectUtil.isNotEmpty(dataDictDetail)) {
                areaInfo.setWaveTypeName(dataDictDetail.getDictValue());
            }
        }
        return list;
//        return areaInfoMapper.findList(criteria);
    }

    @Override
    public List<AreaInfoDto> findByCode(String areaCode) {
        return areaInfoMapper.findByCode(areaCode);
    }

    @Override
    public List<AreaInfoDto> findByName(String areaName) {
        return areaInfoMapper.findByName(areaName);
    }

    @Override
    public int validatorChongfu(ValidatorAreaInfoModel model) {
        return areaInfoMapper.validatorChongfu(model);
    }
}
